#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <set>
using namespace std;
#define debug(x) cout<<#x<<" :"<<x<<endl
struct node
{
    vector<char>st[10];
    set<char>FIRST;
    set<char>FOLLOW;
    int tot;
}a[1100];
char res[1100];
void initfir(char sour) //计算first集
{
    for(int i = 0;i<=a[sour].tot;i++)
    {
        char ins = a[sour].st[i][0];
        a[sour].FIRST.insert(ins);
    }
}
bool judgefir(char sour)
{
    string del;
    string ins;
    for(auto it:a[sour].FIRST)
    {
        if(it>=65 && it<=90)
        {
            for(auto itn:a[it].FIRST) {
                if(itn != '@')  ins += itn;
                else
                {
                    for(int i = 0;i<=a[sour].tot;i++)
                    {
                        if(a[sour].st[i][0]==it)
                        {
                            ins += a[sour].st[i][1];
                        }
                    }
                }

            }
            del += it;
        }
        else ins += it;
    }
    for(int i = 0;i<del.size();i++) a[sour].FIRST.erase(del[i]);
    for(int i = 0;i<ins.size();i++) a[sour].FIRST.insert(ins[i]);
    if(del.size()) return 0;
    else return 1;

}
void initfoll(char sour)
{
    //debug(sour);
    for(int i = 0;i<=a[sour].tot;i++)
    {
        for(int j = 0;j<a[sour].st[i].size();j++)
        {

            char aim = a[sour].st[i][j];
            char aimq;
            char aimh;
            //debug(aim);
            if(j!=0)  aimq = a[sour].st[i][j-1];
            if(j!=a[sour].st[i].size()-1)  aimh = a[sour].st[i][j+1];
            if(aim>=65&&aim<=90)
            {
                if(aimq>=65&&aimq<=90)
                {
                    for(auto it:a[aim].FIRST)
                    {
                        if(it!='@') a[aimq].FOLLOW.insert(it);
                    }
                    for(auto it:a[aim].FOLLOW)
                    {
                        a[aimq].FOLLOW.insert(it);
                    }
                }
                if(aimh<65||aimh>90)
                {
                    //debug(aimh);
                    a[aim].FOLLOW.insert(aimh);
                }

            }
            if(aim>=65&&aim<=90&&j==a[sour].st[i].size()-1)
            {
                for(auto it:a[sour].FOLLOW) a[aim].FOLLOW.insert(it);
            }
        }
    }
}
int main()
{
    string s;
    int cnt = 0;
    while(cin>>s)
    {
        if(s=="Q") break;
        res[cnt++] = s[0];
        int len = s.size();
        a[s[0]].tot = 0;
        for(int i = 3;i<len;i++)
        {
            if(s[i]!='|')
            {
                a[s[0]].st[a[s[0]].tot].push_back(s[i]);
            }
            else a[s[0]].tot++;
        }//初始化
    }
    for(int i = 0;i<cnt;i++) initfir(res[i]); //初始化first
    int num = 0;
    int pos = 0;
    while(num<cnt) //更新first
    {
        if(judgefir(res[pos])) num++;
        pos++;
        if(pos==cnt&&num<cnt)
        {
            num = 0;
            pos = 0;
        }
    }
    a[res[0]].FOLLOW.insert('#');
    for(int i = 0;i<cnt;i++) initfoll(res[i]); // 初始follow
    sort(res,res+cnt);
    for(int i = 0;i<cnt;i++)
    {
       // debug(res[i]);
        string fir;
        string foll;
        int fr = 0;
        int fl = 0;
        for(auto it:a[res[i]].FIRST) {
            if(it !='@')fir += it;
            else fr = 1;
        }
        if(fr) fir += '@';
        for(auto it:a[res[i]].FOLLOW) {
            if(it!='#') foll += it;
            else fl = 1;
        }
        if(fl) foll += '#';
        cout<<res[i]<<" ";
        cout<<"{";
        for(int i = 0;i<fir.size();i++)
        {
            if(i!=fir.size()-1) cout<<fir[i]<<",";
            else cout<<fir[i]<<"} ";
        }
       // debug(foll.size());
        cout<<"{";
        for(int i = 0;i<foll.size();i++)
        {
            if(i!=foll.size()-1) cout<<foll[i]<<",";
            else cout<<foll[i]<<"}\n";
        }
    }
}